<?php

if (!defined('ADMIN_INIT'))
	exit ('Access Denied!');

class admin {

	private $db = null;

	public function __construct($db) {
		$this->db = $db;
	}

	public function __destruct() {
		unset ($this->db);
	}

	public function login($user, $password) {
		$logip = getIP();
		if ($logip != '' && $user != '' && $password != '') {
			$sql = 'CALL ' . DB_NAME_ADMIN . '.sp_admin_login("' . $user . '","' . $password . '","' . $logip . '");';
			$result = $this->getAllSQL($sql);
			if (is_array($result)) {
				return $result[0];
			}
		}
		return false;
	}

	public function getLevel($param_id, $param_pid, $param_sid, $param_name, $param_status, $param_type, $showPage = 0) {
		if ($showPage == 0) {
			$sql = 'CALL ' . DB_NAME_ADMIN . '.sp_admin_getLevel("' . $param_id . '",' . $param_pid . ',' . $param_sid . ',"' . $param_name . '",' . $param_status . ',"' . $param_type . '",NULL,1);';
			$arrRow = $this->getAllSQL($sql);
		}
		elseif ($showPage == 1) {
			$sql = 'CALL ' . DB_NAME_ADMIN . '.sp_admin_getLevel("' . $param_id . '",' . $param_pid . ',' . $param_sid . ',"' . $param_name . '",' . $param_status . ',"' . $param_type . '",NULL,0);';
			$tmpRow = $this->getAllSQL($sql);
			$total = $tmpRow[0][0];
			unset ($tmpRow);
			if ($total > 0) {
				$pageArr = $this->showPage($total, 18);
				$sql = 'CALL ' . DB_NAME_ADMIN . '.sp_admin_getLevel("' . $param_id . '",' . $param_pid . ',' . $param_sid . ',"' . $param_name . '",' . $param_status . ',"' . $param_type . '","' . $pageArr['limit'] . '",1);';
				$arrRow = $this->getAllSQL($sql);
			}
		}
		if (is_array($arrRow)) {
			$arrRow['cnt'] = count($arrRow);
			if ($showPage == 1) {
				$arrRow['page'] = $pageArr['page'];
			}
			return $arrRow;
		}
		return false;
	}

	public function level($id, $pid, $sid, $name, $url, $status, $type) {
		if ($name != '' && $status != '') {
			$sql = 'CALL ' . DB_NAME_ADMIN . '.sp_admin_level(' . $id . ',' . $pid . ',' . $sid . ',"' . $name . '","' . $url . '",' . $status . ',"' . $type . '");';
			$result = $this->executeSQL($sql);
			if ($result != false) {
				return $result;
			}
		}
		return false;
	}

	public function userCreate($parentID, $user, $name, $password, $level, $sublevel, $listlevel) {
		if ($parentID >= 0 && $user != '' && $name != '' && $password != '') {
			$sql = 'CALL ' . DB_NAME_ADMIN . '.sp_admin_userCreate(' . $parentID . ',"' . $user . '","' . $name . '","' . $password . '","' . $level . '","' . $sublevel . '","' . $listlevel . '");';
			$result = $this->executeSQL($sql);
			if ($result != false) {
				return $result;
			}
		}
		return false;
	}

	public function userList($userID) {
		if ($userID >= 0) {
			$sql = 'CALL ' . DB_NAME_ADMIN . '.sp_admin_userList(' . $userID . ');';
			$result = $this->getAllSQL($sql);
			if (is_array($result)) {
				return $result;
			}
		}
		return false;
	}

	public function userFreeze($userID) {
		if ($userID > 0) {
			$sql = 'CALL ' . DB_NAME_ADMIN . '.sp_admin_userFreeze(' . $userID . ');';
			$result = $this->executeSQL($sql);
			if ($result != false) {
				return $result;
			}
		}
		return false;
	}

	public function userResetpwd($userID, $password) {
		if ($userID > 0 && $password != '') {
			$sql = 'CALL ' . DB_NAME_ADMIN . '.sp_admin_userResetpwd(' . $userID . ',"' . $password . '");';
			$result = $this->executeSQL($sql);
			if ($result != false) {
				return $result;
			}
		}
		return false;
	}

	public function userModify($userID, $user, $name, $level, $sublevel, $listlevel) {
		if ($userID > 0 && $user != '' && $name != '') {
			$sql = 'CALL ' . DB_NAME_ADMIN . '.sp_admin_userModify(' . $userID . ',"' . $user . '","' . $name . '","' . $level . '","' . $sublevel . '","' . $listlevel . '");';
			$result = $this->executeSQL($sql);
			if ($result != false) {
				return $result;
			}
		}
		return false;
	}

	public function showUser($userID) {
		if ($userID > 0) {
			$sql = 'CALL ' . DB_NAME_ADMIN . '.sp_admin_showUser(' . $userID . ');';
			$result = $this->getAllSQL($sql);
			if (is_array($result)) {
				return $result[0];
			}
		}
		return false;
	}

	public function userModifypwd($userID, $oldpwd, $pwd) {
		if ($userID > 0 && $oldpwd != '' && $pwd != '') {
			$sql = 'CALL ' . DB_NAME_ADMIN . '.sp_admin_userModifypwd(' . $userID . ',"' . $oldpwd . '","' . $pwd . '");';
			$result = $this->executeSQL($sql);
			if ($result != false) {
				return $result;
			}
		}
		return false;
	}

	public function admin_sysmylog($userID, $total) {
		if ($$userID >= 0 && $total > 0) {
			$pageArr = $this->showPage($total, 20);
			$sql = 'CALL ' . DB_NAME_ADMIN . '.sp_admin_sysmylog(' . $userID . ',"' . $pageArr['limit'] . '");';
			$result = $this->getAllSQL($sql);
			if (is_array($result)) {
				$result['cnt'] = count($result);
				$result['page'] = $pageArr['page'];
				return $result;
			}
		}
		return false;
	}

	public function showCase($caseID = 0) {
		$sql = 'CALL ' . DB_NAME_ADMIN . '.sp_admin_showCase(' . $caseID . ');';
		$result = $this->getAllSQL($sql);
		if (is_array($result)) {
			return $result;
		}
		return false;
	}

	public function admin_syslog($userID, $wd, $rID, $cID, $oID) {
		if ($rID >= 0 && $cID >= 0 && $oID >= 0) {
			$sql = 'CALL ' . DB_NAME_ADMIN . '.sp_admin_syslog(' . $userID . ',"' . $wd . '",' . $rID . ',' . $cID . ',' . $oID . ',NULL,1);';
			$tmpRow = $this->getAllSQL($sql);
			$total = $tmpRow[0][0];
			unset ($tmpRow);
			if ($total > 0) {
				$pageArr = $this->showPage($total, 18);
				$sql = 'CALL ' . DB_NAME_ADMIN . '.sp_admin_syslog(' . $userID . ',"' . $wd . '",' . $rID . ',' . $cID . ',' . $oID . ',"' . $pageArr['limit'] . '",2);';
				$arrRow = $this->getAllSQL($sql);
			}
			if (is_array($arrRow)) {
				$arrRow['cnt'] = count($arrRow);
				$arrRow['page'] = $pageArr['page'];
				return $arrRow;
			}
		}
		return false;
	}

	private function executeSQL($sql) {
		if ($sql != '') {
			$result = $this->db->Execute($sql);
			if (is_object($result)) {
				$arrRow = $result->FetchRow();
				$this->db->NextResult();
				return $arrRow[0];
			}
		}
		return false;
	}

	private function getAllSQL($sql) {
		if ($sql != '') {
			$result = $this->db->GetAll($sql);
			if (is_array($result)) {
				$this->db->NextResult();
				return $result;
			}
		}
		return false;
	}

	private function showPage($total, $perpage = 10) {
		if ($total > 0) {
			require_once ('page.class.php');
			$page = new extpage(array (
				'total' => $total,
				'perpage' => $perpage
			));
			$pageArr['page'] = $page->show();
			$pageArr['limit'] = $page->limit();
			unset ($page);
		}
		return $pageArr;
	}
}
?>